package com.train.course.service.impl;

import com.train.course.domain.VideoTag;
import com.train.course.domain.VideoTagRelation;
import com.train.course.dto.CourseTagDTO;
import com.train.course.mapper.VideoTagMapper;
import com.train.course.mapper.VideoTagRelationMapper;
import com.train.course.service.IVideoTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 视频标签Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class VideoTagServiceImpl implements IVideoTagService 
{
    @Autowired
    private VideoTagMapper videoTagMapper;

    @Autowired
    private VideoTagRelationMapper videoTagRelationMapper;

    /**
     * 查询视频标签
     * 
     * @param tagId 视频标签主键
     * @return 视频标签
     */
    @Override
    public VideoTag selectVideoTagById(Long tagId)
    {
        return videoTagMapper.selectVideoTagById(tagId);
    }

    /**
     * 查询视频标签列表
     * 
     * @param videoTag 视频标签
     * @return 视频标签
     */
    @Override
    public List<VideoTag> selectVideoTagList(VideoTag videoTag)
    {
        return videoTagMapper.selectVideoTagList(videoTag);
    }

    /**
     * 新增视频标签
     * 
     * @param videoTag 视频标签
     * @return 结果
     */
    @Override
    public int insertVideoTag(VideoTag videoTag)
    {
        return videoTagMapper.insertVideoTag(videoTag);
    }

    /**
     * 修改视频标签
     * 
     * @param videoTag 视频标签
     * @return 结果
     */
    @Override
    public int updateVideoTag(VideoTag videoTag)
    {
        return videoTagMapper.updateVideoTag(videoTag);
    }

    /**
     * 批量删除视频标签
     * 
     * @param tagIds 需要删除的视频标签主键
     * @return 结果
     */
    @Override
    public int deleteVideoTagByIds(Long[] tagIds)
    {
        return videoTagMapper.deleteVideoTagByIds(tagIds);
    }

    /**
     * 删除视频标签信息
     * 
     * @param tagId 视频标签主键
     * @return 结果
     */
    @Override
    public int deleteVideoTagById(Long tagId)
    {
        return videoTagMapper.deleteVideoTagById(tagId);
    }

    /**
     * 为课程添加标签
     * 
     * @param courseTagDTO 课程标签DTO
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addCourseTags(CourseTagDTO courseTagDTO)
    {
        Long courseId = courseTagDTO.getCourseId();
        List<String> tagNames = courseTagDTO.getTagNames();
        
        // 先删除原有的标签关联（课程类型为1）
        videoTagRelationMapper.deleteVideoTagRelationByVideoId(courseId, "1");
        
        int result = 0;
        for (String tagName : tagNames) {
            if (tagName == null || tagName.trim().isEmpty()) {
                continue;
            }
            
            // 查询标签是否已存在
            VideoTag existTag = videoTagMapper.selectVideoTagByName(tagName.trim());
            Long tagId;
            
            if (existTag == null) {
                // 标签不存在，创建新标签
                VideoTag newTag = new VideoTag();
                newTag.setTagName(tagName.trim());
                newTag.setStatus("0"); // 正常状态
                videoTagMapper.insertVideoTag(newTag);
                tagId = newTag.getTagId();
            } else {
                tagId = existTag.getTagId();
            }
            
            // 创建课程与标签的关联
            VideoTagRelation relation = new VideoTagRelation();
            relation.setVideoId(courseId);
            relation.setTagId(tagId);
            relation.setVideoType("1"); // 1表示课程
            result += videoTagRelationMapper.insertVideoTagRelation(relation);
        }
        
        return result;
    }

    /**
     * 根据视频ID查询标签列表
     * 
     * @param videoId 视频ID
     * @param videoType 视频类型
     * @return 标签列表
     */
    @Override
    public List<VideoTag> selectTagsByVideoId(Long videoId, String videoType)
    {
        return videoTagMapper.selectTagsByVideoId(videoId, videoType);
    }
}
